package csu.web.mypetstore.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SignOnFormServlet extends HttpServlet {

    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理 POST 请求，比如表单提交
        // 你可以在这里添加处理逻辑，比如验证用户登录等
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 示例：简单的验证
        if ("admin".equals(username) && "password".equals(password)) {
            // 登录成功，重定向到主页面
            resp.sendRedirect("homepage");
        } else {
            // 登录失败，返回到登录页面并提示错误
            req.setAttribute("errorMessage", "Invalid username or password.");
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
        }
    }
}